package org.example.model;

import org.example.business.UserBusiness;

import javax.swing.*;
import java.awt.*;
import java.io.*;

public class User {
    public String mailbox, password,
            avatarPath, name,
            signature, identity,
            orderPath, condition;
    public User() {
        name = "用户200";
        identity = "普通用户";
        signature = "";
        condition = "正常";
    }
    public void setAvatarPath() {
        if (UserBusiness.user != null)
            avatarPath = "background/" + UserBusiness.user.mailbox;
        else avatarPath = "background/" + mailbox;
        try {
            FileInputStream reader = new FileInputStream(new File("data/background/img.png"));
            File file = new File("data/" + avatarPath);
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream writer = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int n;
            while ((n = reader.read(bytes)) != -1) {
                writer.write(bytes, 0, n);
            }
            reader.close();
            writer.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public void savePhoto() {
        FileDialog dialog = new FileDialog(new JFrame(), "选择图片", FileDialog.LOAD);
        dialog.setVisible(true);
        String imagePath = dialog.getDirectory() + dialog.getFile();
        try {
            FileInputStream reader = new FileInputStream(new File(imagePath));
            FileOutputStream writer = new FileOutputStream(new File("data/" +UserBusiness.user.avatarPath));
            byte[] bytes = new byte[1024];
            int n;
            while ((n = reader.read(bytes)) != -1) {
                writer.write(bytes, 0, n);
            }
            reader.close();
            writer.close();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
